home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / util / misc / filewatcher01.lha / filewatcher / filewatcher.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  7KB  |  307 lines

  1. #include <dos/dos.h>
  2. #include <dos/notify.h>
  3. #include <dos/dostags.h>
  4. #include <exec/ports.h>
  5. #include <workbench/icon.h>
  6.  
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/alib_protos.h>
  10. #include <clib/icon_protos.h>
  11. #include <clib/wb_protos.h>
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <signal.h>
  16.  
  17. /*
  18.  * Version String
  19.  */
  20.  
  21. STRPTR Version = "$VER: FileWatcher 0.1 (23.04.95) © Tom Hayko";
  22.  
  23. /*
  24.  * ReadArgs() Template
  25.  */
  26.  
  27. STRPTR ArgTemplate = "FILE/AK,FILEICON/AK,NOFILEICON/AK,NAME/K,CMD/K";
  28.  
  29. /*
  30.  * Define Name under AppIcon
  31.  */
  32.  
  33. #define DEF_NAME    "FileWatcher"
  34.  
  35. /*
  36.  * Local Prototypes
  37.  */
  38.  
  39. BOOL GetFileSize(STRPTR FileName, ULONG * FileSize);
  40.  
  41. main(int argc, char **argv)
  42. {
  43.     int i;
  44.     BOOL bSuccess;
  45.     struct MsgPort *msgport;
  46.     struct MsgPort *appmsgport;
  47.     struct NotifyRequest notifyreq;
  48.     struct Message *msg;
  49.     struct DiskObject *diskobjFile;
  50.     struct DiskObject *diskobjNoFile;
  51.     struct AppIcon *appicon;
  52.     ULONG filesize;
  53.     ULONG newfilesize;
  54.     ULONG sig;
  55.     ULONG sigcntrlc;
  56.     ULONG sigmsg;
  57.     ULONG sigappmsg;
  58.     ULONG lRC;
  59.     struct RDArgs *rdargs;
  60.     LONG Args[5];
  61.  
  62.     for (i = 0; i < (sizeof(Args) / sizeof(LONG)); i++)
  63.     {
  64.         Args[i] = 0;
  65.     }
  66.  
  67.     Args[3] = (LONG) DEF_NAME;
  68.     Args[4] = (LONG) "";
  69.  
  70.     rdargs = ReadArgs(ArgTemplate, Args, NULL);
  71.  
  72.     if (rdargs == NULL)
  73.     {
  74.         printf("%s: required argument missing\n", argv[0]);
  75.         exit(RETURN_FAIL);
  76.     }
  77.  
  78.     signal(SIGINT, SIG_IGN);
  79.     signal(SIGTERM, SIG_IGN);
  80.  
  81.     diskobjFile = GetDiskObject((char *) Args[1]);
  82.  
  83.     if (diskobjFile == NULL)
  84.     {
  85.         printf("%s: couldn't load icon '%s'\n", argv[0], Args[1]);
  86.         exit(RETURN_FAIL);
  87.     }
  88.  
  89.     diskobjNoFile = GetDiskObject((char *) Args[2]);
  90.  
  91.     if (diskobjNoFile == NULL)
  92.     {
  93.         printf("%s: couldn't load icon '%s'\n", argv[0], Args[2]);
  94.         FreeDiskObject(diskobjFile);
  95.         exit(RETURN_FAIL);
  96.     }
  97.  
  98.     msgport = CreatePort((STRPTR) NULL, 0);
  99.  
  100.     if (msgport == NULL)
  101.     {
  102.         printf("%s: couldn't CreatePort()\n", argv[0]);
  103.         FreeDiskObject(diskobjNoFile);
  104.         FreeDiskObject(diskobjFile);
  105.         exit(RETURN_FAIL);
  106.     }
  107.  
  108.     appmsgport = CreatePort((STRPTR) NULL, 0);
  109.  
  110.     if (appmsgport == NULL)
  111.     {
  112.         printf("%s: couldn't CreatePort()\n", argv[0]);
  113.         DeletePort(msgport);
  114.         FreeDiskObject(diskobjNoFile);
  115.         FreeDiskObject(diskobjFile);
  116.         exit(0);
  117.     }
  118.  
  119.     bSuccess = GetFileSize((STRPTR) Args[0], &filesize);
  120.  
  121.     if (!bSuccess)
  122.     {
  123.         printf("%s: couldn't get size of file '%s'\n", argv[0], (char *) Args[0]);
  124.         DeletePort(msgport);
  125.         FreeDiskObject(diskobjNoFile);
  126.         FreeDiskObject(diskobjFile);
  127.         exit(0);
  128.     }
  129.  
  130.     notifyreq.nr_Name = (UBYTE *) Args[0];
  131.     notifyreq.nr_Flags = NRF_SEND_MESSAGE;
  132.     notifyreq.nr_stuff.nr_Msg.nr_Port = msgport;
  133.  
  134.     bSuccess = StartNotify(¬ifyreq);
  135.  
  136.     if (!bSuccess)
  137.     {
  138.         printf("StartNotify() failed\n");
  139.         DeletePort(msgport);
  140.         FreeDiskObject(diskobjNoFile);
  141.         FreeDiskObject(diskobjFile);
  142.         exit(0);
  143.     }
  144.  
  145.     if (filesize != 0)
  146.     {
  147.         appicon = AddAppIcon(0L,
  148.                              0L,
  149.                              (char *) Args[3],
  150.                              appmsgport,
  151.                              NULL,
  152.                              diskobjFile,
  153.                              TAG_DONE);
  154.     }
  155.     else
  156.     {
  157.         appicon = AddAppIcon(0L,
  158.                              0L,
  159.                              (char *) Args[3],
  160.                              appmsgport,
  161.                              NULL,
  162.                              diskobjNoFile,
  163.                              TAG_DONE);
  164.     }
  165.  
  166.     if (appicon == NULL)
  167.     {
  168.         printf("AddAppIcon() failed\n");
  169.         EndNotify(¬ifyreq);
  170.  
  171.         while (msg = GetMsg(msgport))
  172.             ReplyMsg(msg);
  173.  
  174.         DeletePort(appmsgport);
  175.         DeletePort(msgport);
  176.         exit(0);
  177.     }
  178.  
  179.     sigcntrlc = 1L << SIGBREAKB_CTRL_C;
  180.     sigmsg = 1L << msgport->mp_SigBit;
  181.     sigappmsg = 1L << appmsgport->mp_SigBit;
  182.  
  183.     while (sig = Wait(sigcntrlc | sigmsg | sigappmsg))
  184.     {
  185.         if (sig & sigcntrlc)
  186.         {
  187.             break;
  188.         }
  189.  
  190.         if (sig & sigmsg)
  191.         {
  192.             while (msg = GetMsg(msgport))
  193.             {
  194.                 ReplyMsg(msg);
  195.             }
  196.  
  197.             bSuccess = GetFileSize((STRPTR)Args[0], &newfilesize);
  198.  
  199.             RemoveAppIcon(appicon);
  200.  
  201.             if (newfilesize != 0)
  202.             {
  203.                 appicon = AddAppIcon(0L,
  204.                                      0L,
  205.                                      (char *) Args[3],
  206.                                      appmsgport,
  207.                                      NULL,
  208.                                      diskobjFile,
  209.                                      TAG_DONE);
  210.             }
  211.             else
  212.             {
  213.                 appicon = AddAppIcon(0L,
  214.                                      0L,
  215.                                      (char *) Args[3],
  216.                                      appmsgport,
  217.                                      NULL,
  218.                                      diskobjNoFile,
  219.                                      TAG_DONE);
  220.             }
  221.  
  222.             filesize = newfilesize;
  223.         }
  224.  
  225.         if (sig & sigappmsg)
  226.         {
  227.             while (msg = GetMsg(appmsgport))
  228.             {
  229.                 ReplyMsg(msg);
  230.             }
  231.  
  232.             if(strcmp((STRPTR) Args[4], "") != 0)
  233.             {
  234.                 printf("spawning command '%s'\n", (STRPTR) Args[4]);
  235.  
  236.                 lRC = SystemTags((STRPTR) Args[4], SYS_Asynch, 0, TAG_DONE, 0);
  237.  
  238.                 if(lRC != 0)
  239.                 {
  240.                     printf("Error spawning command '%s'\n", (STRPTR) Args[5]);
  241.                     break;
  242.                 }
  243.             }
  244.         }
  245.     }
  246.  
  247.     EndNotify(¬ifyreq);
  248.  
  249.     RemoveAppIcon(appicon);
  250.  
  251.     while (msg = GetMsg(appmsgport))
  252.         ReplyMsg(msg);
  253.  
  254.     DeletePort(appmsgport);
  255.  
  256.     while (msg = GetMsg(msgport))
  257.         ReplyMsg(msg);
  258.  
  259.     DeletePort(msgport);
  260.  
  261.     FreeDiskObject(diskobjNoFile);
  262.     FreeDiskObject(diskobjFile);
  263.  
  264.     FreeArgs(rdargs);
  265.  
  266.     exit(0);
  267. }
  268.  
  269. BOOL GetFileSize(STRPTR FileName, ULONG * FileSize)
  270. {
  271.     BPTR bptrLock;
  272.     struct FileInfoBlock *pfileinfoblock;
  273.     BOOL fSuccess;
  274.  
  275.     bptrLock = Lock(FileName, ACCESS_READ);
  276.  
  277.     if (bptrLock == 0)
  278.     {
  279.         *FileSize = 0L;
  280.         return (TRUE);
  281.     }
  282.  
  283.     pfileinfoblock = (struct FileInfoBlock *) AllocDosObjectTags(DOS_FIB, TAG_DONE);
  284.  
  285.     if (pfileinfoblock == NULL)
  286.     {
  287.         UnLock(bptrLock);
  288.         return (FALSE);
  289.     }
  290.  
  291.     fSuccess = Examine(bptrLock, pfileinfoblock);
  292.  
  293.     if (!fSuccess)
  294.     {
  295.         FreeDosObject(DOS_FIB, pfileinfoblock);
  296.         UnLock(bptrLock);
  297.         return (FALSE);
  298.     }
  299.  
  300.     *FileSize = pfileinfoblock->fib_Size;
  301.  
  302.     FreeDosObject(DOS_FIB, pfileinfoblock);
  303.     UnLock(bptrLock);
  304.  
  305.     return (TRUE);
  306. }
  307.